import queue
import threading
import time

from api_audio import AudioPlay
from send_micro_weight import send_azure_blendshape_weight2


class SoundAnimation:
    def __init__(self):
        self.weight_queue = queue.SimpleQueue()
        self.animation_queue = queue.SimpleQueue()
        self.start_processing()

    def play_work(self,weight_list,audio_stream_list):
        player = AudioPlay()
        bytes_merge = b''
        for each in audio_stream_list:
            bytes_merge += each
        player.play_audio_data_thread(bytes_merge)
        start_time = time.time()
        for each in weight_list:
            send_azure_blendshape_weight2(each,start_time)
    def put_data(self,weight_list,audio_stream_list):
        self.weight_queue.put_nowait(weight_list)
        self.animation_queue.put_nowait(audio_stream_list)
    def play_thread(self):
        while True:
            sound_list = self.animation_queue.get(block=True)
            weight_list = self.weight_queue.get(block=True)
            self.play_work(weight_list,sound_list)
    def start_processing(self):
        self.play_thread_var = threading.Thread(target=self.play_thread)
        self.play_thread_var.setDaemon(True)
        self.play_thread_var.start()